package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.api.Request;
import cc.iotkit.common.enums.ErrCode;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.system.ISysOrderData;
import cc.iotkit.data.util.OrderUtil;
import cc.iotkit.manager.dto.bo.order.OrderBo;
import cc.iotkit.manager.dto.vo.miniAdmin.MiniAdminOrderStatisRspVo;
import cc.iotkit.manager.dto.vo.miniAdmin.MiniAdminOrderVo;
import cc.iotkit.manager.service.IOrderService;
import cc.iotkit.model.miniAdmin.OrderStatis;
import cc.iotkit.model.system.SysOrder;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class OrderServiceImpl implements IOrderService {

    private final ISysOrderData iSysOrderData;
    @Override
    public void addOrder(OrderBo orderBo) {
        SysOrder order = new SysOrder();
        BeanUtils.copyProperties(orderBo,order);

        order.setOrderNo(OrderUtil.generateOrderNumber());
        order.setStartTime(DateTime.now());
        // 借用时长
        // TODO 开启柜门

        iSysOrderData.save(order);
    }

    public void updateOrder(OrderBo orderBo) {
        if(ObjectUtil.isEmpty(orderBo) || ObjectUtil.isEmpty(orderBo.getId())){
            throw new BizException("订单为空或订单ID为空");
        }
        SysOrder oldOrder =  iSysOrderData.findById(orderBo.getId());
        if(ObjectUtil.isEmpty(oldOrder) ){
            throw new BizException("订单为空");
        }

        SysOrder order = new SysOrder();
        BeanUtils.copyProperties(orderBo,order);

        //order.setOrderNo(OrderUtil.generateOrderNumber());
        order.setEndTime(DateTime.now());

        // 借用时长
        // TODO 关闭柜门
        iSysOrderData.save(order);
    }




    @Override
    public SysOrder getOrderById(Long id) {



        SysOrder sysOrder = iSysOrderData.findById(id);

        return sysOrder;
    }

    @Override
    public Paging<SysOrder> getPageList(PageRequest<MiniAdminOrderVo> query) throws Exception {

        MiniAdminOrderVo orderVo =  query.getData();
        String orderNo = null;
        String endTime = null;
        String startTime =null;
        if(ObjectUtil.isNotEmpty(orderVo)) {
            orderNo = orderVo.getOrderNo();
            startTime = orderVo.getStartTime();
            endTime = orderVo.getEndTime();
        }
       return iSysOrderData.findByConditions(orderNo,startTime,endTime,query.getOffset(),query.getPageSize());
    }


    @Override
    public MiniAdminOrderStatisRspVo getCountStatis(PageRequest<MiniAdminOrderVo> query) throws Exception{
        MiniAdminOrderStatisRspVo rspVo = new MiniAdminOrderStatisRspVo();
        MiniAdminOrderVo orderVo =  query.getData();
        String orderNo = null;
        String endTime = null;
        String startTime =null;
        if(ObjectUtil.isNotEmpty(orderVo)) {
             orderNo = orderVo.getOrderNo();
             startTime = orderVo.getStartTime();
             endTime = orderVo.getEndTime();
        }
        OrderStatis statis = iSysOrderData.getCountStatis(orderNo,startTime,endTime,query.getOffset(),query.getPageSize());
        BeanUtils.copyProperties(statis,rspVo);
        return rspVo;
    }

    @Override
    public SysOrder getById(Request<Long> orderBoRequest) {
        return iSysOrderData.findById(orderBoRequest.getData());
    }
}
